package Controler.Eleve;

import java.io.IOException;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import JDBC.Eleve_Ajouter;
import Model.Eleve;
import Model.Famille;
import Model.Lieu;

@WebServlet("/Eleve_AjouterController")

public class Eleve_AjouterController  extends HttpServlet {
	
		private static final long serialVersionUID = 1L;
	       
	    public Eleve_AjouterController() 
	    {
	        super();
	    }
	    
	
		protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
		{
			String erreur = "";
			String JSPURL = "GEleve.jsp?error=";
			//int id= request.getParameter("id");
			
			String nom = request.getParameter("nom");
			String prenom = request.getParameter("prenom");
			String sexe = request.getParameter("sexe");
			String date_naiss = request.getParameter("date_naiss");
			String mail = request.getParameter("mail");
			String tel =  request.getParameter("tel");
			String nomP = request.getParameter("nomP");
			String prenomP =  request.getParameter("prenomP");
			String mailP =  request.getParameter("mailP");
			String telP =  request.getParameter("telP");
			String numeroF = request.getParameter("numeroF");
			String rueF = request.getParameter("rueF");
			String cpF = request.getParameter("cpF");
			String villeF = request.getParameter("villeF");
			String compF = request.getParameter("compF");
			
			int nump=0;				
			int num=0;
			int codepostal=0;
			
			try
			{
				nump = Integer.parseInt(telP);
			
			}
			catch(NumberFormatException nfe)
			{
				JSPURL = "index.jsp?error=";
				erreur = "Entrez un numero de telephone valide";
			}
			
			try
			{
				num = Integer.parseInt(numeroF);
			
			}
			catch(NumberFormatException nfe)
			{
				JSPURL = "index.jsp?error=";
				erreur = "Entrez un numero de rue valide";
			}
			
			try
			{
				codepostal = Integer.parseInt(cpF);
			
			}
			catch(NumberFormatException nfe)
			{
				JSPURL = "index.jsp?error=";
				erreur = "Entrez un code postal valide";
			}
			
			if(nom.compareTo("") == 0)erreur = "Entrez un nom";
			else if(prenom.compareTo("") == 0)erreur = "Entrez un prenom";
			else if(sexe.compareTo("") == 0)erreur = "Entrez un sexe";
			else if(rueF.compareTo("") == 0)erreur = "Entrez une rue";
			else if(villeF.compareTo("") == 0)erreur = "Entrez une ville";
			else if(date_naiss.compareTo("") == 0)erreur = "Entrez une date de naissance";
			else JSPURL = "Vues/Eleve/vueGRP1_afficher.jsp";
					
			Eleve e = new Eleve();
			Famille f = new Famille();
			Lieu a = new Lieu();
			
			a.numero = num;
			a.rue = rueF;
			a.cp = codepostal;
			a.ville = villeF;
			a.complement=compF;
			a.idDepartement=1;
			
			e.setNom(nom);
			e.setPrenom(prenom);
			e.setSexe(sexe);
			e.setNaissance(date_naiss);
			e.setEmail(mail);
			e.setTel(tel);
			
			f.setNom(nomP);
			f.setPrenom(prenomP);
			f.setMail(mailP);
			f.setNum(nump);
			
			e.setId_famille(f);
			
			e.setAdresse_cours(a);
			f.setAdresse_fac(a);
			
			Eleve_Ajouter dada = new Eleve_Ajouter();
			dada.ajouter(e);
			
			request.setAttribute("eleve", e);
			RequestDispatcher rd = request.getRequestDispatcher(JSPURL+erreur);
			rd.forward(request, response);
	
		}

		protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
		{
			doPost(request,response);
		}

	}

	
	

